/**
 * 
 */
package com.vector.ding.commons.util.front;

import java.util.List;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.github.pagehelper.Page;
import com.vector.ding.commons.util.StringUtil;
import com.vector.ding.commons.util.collection.CollectionUtil;
import com.vector.ding.context.WebContext;
import com.vector.ding.dao.bean.MenuEntity;

/**
 * @author vector
 *
 */
public class BuiUtil {
	
	//private static final String HOME_PAGE_ID = "";
	
	public static JSONArray menuAdapter(
			List<MenuEntity> menuEntities, Set<String> authMenuSet) {
		JSONArray menuConfig = new JSONArray();
		if (CollectionUtil.isEmpty(menuEntities) || (authMenuSet != null && authMenuSet.isEmpty()))
			return menuConfig;
		
//		boolean needAuth = authMenuSet == null ? false : true;
		JSONObject menuJson = new JSONObject();
		menuJson.put("id", "menu");
		JSONArray menuJsonArr = new JSONArray();
		for (MenuEntity menuEntity: menuEntities) {
//			String menuId = menuEntity.getMenu().getId();
//			if (needAuth && !authMenuSet.contains(menuId))
//				continue;
			JSONObject menuModule = getMenuModule(menuEntity, authMenuSet);
			if (menuModule == null)
				continue;
			menuJsonArr.add(menuModule);
		}
		
		menuJson.put("menu", menuJsonArr);
		menuConfig.add(menuJson);
		return menuConfig;
	}
	
	public static JSONArray menuTreeAdapter(
			List<MenuEntity> menuEntities, Set<String> authMenuSet) {
		JSONArray menus = new JSONArray(); 
		if (CollectionUtil.isEmpty(menuEntities))
			return menus;
		
//		boolean needAuth = authMenuSet == null ? false : true;
		for (MenuEntity menuEntity: menuEntities) {
//			String menuId = menuEntity.getMenu().getId();
//			if (needAuth && !authMenuSet.contains(menuId))
//				continue;
			JSONObject menuItem = getMenuTreeItem(menuEntity, authMenuSet);
			if (menuItem != null)
				menus.add(menuItem);
		}
		return menus;
	}
	
	public static JSONObject gridAdapter(List<?> data) {
		JSONObject result = new JSONObject();
		JSONArray jsonData = JSONArray.fromObject(data);
		if (null == jsonData)
			return result;
		
		result.put("rows", jsonData);
		if (data instanceof Page)
			result.put("results", ((Page<?>)data).getTotal());
		else 
			result.put("results", jsonData.size());
		result.put("hasError", false);
		//result.put("error", "asda");
		return result;
	}
	
	public static JSONObject gridAdapter(List<?> data, int total) {
		JSONObject result = new JSONObject();
		JSONArray jsonData = JSONArray.fromObject(data);
		if (null == jsonData)
			return result;
		
		result.put("rows", jsonData);
		result.put("results", total);
		result.put("hasError", false);
		//result.put("error", "asda");
		return result;
	}
	
	private static JSONObject getMenuModule(MenuEntity menuEntity, Set<String> authMenuSet) {
		List<MenuEntity> menuItems = menuEntity.getChildren();
		if (CollectionUtil.isEmpty(menuItems))
			return null;
		
		JSONObject menuModule = new JSONObject();
		menuModule.put("text", menuEntity.getMenu().getName());
		JSONArray items = new JSONArray();
		for (MenuEntity menuItem: menuItems) {
			String menuId = menuItem.getMenu().getId();
			if (authMenuSet != null && !authMenuSet.contains(menuId))
				continue;
			JSONObject item = new JSONObject();
			item.put("id", menuItem.getMenu().getId());
			item.put("text", menuItem.getMenu().getName());
			item.put("href", wrapUrl(menuItem.getMenu().getUrl()));
			items.add(item);
		}
		if (items.isEmpty())
			return null;
		
		menuModule.put("items", items);
		return menuModule;
	}
	
	private static JSONObject getMenuTreeItem(MenuEntity menuEntity,
			Set<String> menuAuthSet) {
		List<MenuEntity> menuItems = menuEntity.getChildren();
		if (CollectionUtil.isEmpty(menuItems))
			return null;
		
		JSONObject menuModule = new JSONObject();
		menuModule.put("id", menuEntity.getMenu().getId());
		menuModule.put("text", menuEntity.getMenu().getName());
		menuModule.put("expanded", true);
		JSONArray items = new JSONArray();
		for (MenuEntity menuItem: menuItems) {
			String menuId = menuItem.getMenu().getId();
			JSONObject item = new JSONObject();
			item.put("id", menuItem.getMenu().getId());
			item.put("text", menuItem.getMenu().getName());
			item.put("leaf", true);
			if (menuAuthSet != null && menuAuthSet.contains(menuId))
				item.put("checked", true);
			items.add(item);
		}
		menuModule.put("children", items);
		
		return menuModule;
	}
	
	private static String wrapUrl(String menuUrl) {
		if (StringUtil.isEmpty(menuUrl))
			return "";
		
		if (!menuUrl.startsWith("/"))
			menuUrl = "/" + menuUrl;
		if (!menuUrl.startsWith(WebContext.getContextPath()))
			menuUrl = WebContext.getContextPath() + menuUrl;
		return menuUrl;
	}
}
